﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using AppLayer;
using SpotItServerAPI;
using SpotItComponents;

using NUnit.Framework;

namespace SpotIt
{
    /// <summary>
    /// Interaction logic for SpotIt_Symbol_Set_Manager.xaml
    /// </summary>
    public partial class SpotIt_Symbol_Set_Manager : Window
    {
        Session session;
        Services services;
        List<Deck> decks;
        Deck selectedDeck;
        AudioFiles theAudioFiles = AudioFiles.Instance;
        public SpotIt_Symbol_Set_Manager(Session sessions, Services service)
        {
            this.session = sessions;
            this.services = service;
            InitializeComponent();

            editSetButton.IsEnabled = false;
            twoPanelSelector.SymbolSetSelected += new EventHandler(enableEditButton);
            twoPanelSelector.SymbolSetUnselected += new EventHandler(disableEditButton);
        }

        public void disableEditButton(object sender, EventArgs e)
        {
            editSetButton.IsEnabled = false;
            createDeckButton.IsEnabled = false;
        }

        public void enableEditButton(object sender, EventArgs e)
        {
            editSetButton.IsEnabled = true;
            if (deckDataGrid.SelectedItem != null)
                createDeckButton.IsEnabled = true;
            else
                createDeckButton.IsEnabled = false;
        }

        private void editSetButton_Click(object sender, RoutedEventArgs e)
        {
            theAudioFiles.theFiles[2].Play();
            SpotIt_Deck_Editor deckEditor = new SpotIt_Deck_Editor(session, services);
            this.Hide();
            deckEditor.CurrentSymbolSet = twoPanelSelector.SelectedSymbolSet;
            deckEditor.ShowDialog();
            twoPanelSelector.loadGrids();
            this.ShowDialog();
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            twoPanelSelector.services = services;
            twoPanelSelector.session = session;
            twoPanelSelector.loadGrids();

            deckDataGrid.CanUserAddRows = false;
            deckDataGrid.CanUserDeleteRows = false;
            deckDataGrid.CanUserResizeColumns = false;

            createDeckButton.IsEnabled = false;
            clearDeckButton.IsEnabled = false;

            decks = new List<Deck>();
            OperationResult result = services.deckManager.Lookup(session, null, null, null);
            if (result.Success)
                decks = result.Data as List<Deck>;

            deckDataGrid.ItemsSource = decks;
        }

        private void newCategoryButton_Click(object sender, RoutedEventArgs e)
        {
            theAudioFiles.theFiles[2].Play();
            if (newCategoryTextField.Text != null)
                twoPanelSelector.addNewCategory(newCategoryTextField.Text);
            newCategoryTextField.Text = "";
            newCategoryTextField.Foreground = Brushes.DarkGray;
            newCategoryTextField.Text = "New Category";
        }

        private void addNewSymbolSetButton_Click(object sender, RoutedEventArgs e)
        {
            theAudioFiles.theFiles[2].Play();
            if (newSymbolSetTextField.Text != null)
                twoPanelSelector.addNewSymbolSet(newSymbolSetTextField.Text);
            newSymbolSetTextField.Text = "";
            newSymbolSetTextField.Foreground = Brushes.DarkGray;
            newSymbolSetTextField.Text = "New Symbol Set";
        }

        private void deleteCategoryButton_Click_1(object sender, RoutedEventArgs e)
        {
            theAudioFiles.theFiles[2].Play();
            twoPanelSelector.deleteSelectedCategory();
        }

        private void deleteSetButton_Click_1(object sender, RoutedEventArgs e)
        {
            theAudioFiles.theFiles[2].Play();
            twoPanelSelector.deleteSelectedSymbolSet();
        }

        private void newCategoryTextField_GotFocus(object sender, RoutedEventArgs e)
        {
            if (newCategoryTextField.Foreground != Brushes.Black)
            {
                newCategoryTextField.Foreground = Brushes.Black;
                newCategoryTextField.Text = "";
            }
        }

        private void newSymbolSetTextField_GotFocus(object sender, RoutedEventArgs e)
        {
            if (newSymbolSetTextField.Foreground != Brushes.Black)
            {
                newSymbolSetTextField.Foreground = Brushes.Black;
                newSymbolSetTextField.Text = "";
            }
        }


        private void newCategoryTextField_LostFocus(object sender, RoutedEventArgs e)
        {
            if (newCategoryTextField.Text == "")
            {
                newCategoryTextField.Foreground = Brushes.DarkGray;
                newCategoryTextField.Text = "New Category";
            }
        }

        private void newSymbolSetTextField_LostFocus(object sender, RoutedEventArgs e)
        {
            if (newSymbolSetTextField.Text == "")
            {
                newSymbolSetTextField.Foreground = Brushes.DarkGray;
                newSymbolSetTextField.Text = "New Symbol Set";
            }
        }

        private void updateDeckDataGrid()
        {
            decks = new List<Deck>();
            OperationResult result = services.deckManager.Lookup(session, null, null, null);
            if (result.Success)
                decks = result.Data as List<Deck>;

            deckDataGrid.ItemsSource = decks;
        }

        private void newDeckButton_Click(object sender, RoutedEventArgs e)
        {
            theAudioFiles.theFiles[2].Play();
            if (newDeckTextBox.Foreground != Brushes.DarkGray)
            {
                Deck newDeck = new Deck(newDeckTextBox.Text, false);
                services.deckManager.Add(session, newDeck);

                newDeckTextBox.Text = "New Deck";
                newDeckTextBox.Foreground = Brushes.DarkGray;

                updateDeckDataGrid();
            }
        }

        private void deleteDeckButton_Click(object sender, RoutedEventArgs e)
        {
            theAudioFiles.theFiles[2].Play();
            if (selectedDeck != null)
            {
                services.deckManager.Remove(session, selectedDeck);

                OperationResult result = services.deckManager.Lookup(session, null, null, null);
                if (result.Success)
                    decks = result.Data as List<Deck>;

                deckDataGrid.ItemsSource = decks;
            }

        }

        private void createDeckButton_Click(object sender, RoutedEventArgs e)
        {
            theAudioFiles.theFiles[2].Play();
            if (selectedDeck != null && twoPanelSelector.SelectedSymbolSet != null)
            {
                SpotIt_SetupDeckWindow setupDirectorWindow = 
                    new SpotIt_SetupDeckWindow(services, session, selectedDeck, twoPanelSelector.SelectedSymbolSet);
                setupDirectorWindow.ShowDialog();
            }
        }

        private void clearDeckButton_Click(object sender, RoutedEventArgs e)
        {
            theAudioFiles.theFiles[2].Play();
        }

        private void newDeckTextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            if (newDeckTextBox.Foreground != Brushes.Black)
            {
                newDeckTextBox.Text = "";
                newDeckTextBox.Foreground = Brushes.Black;
            }
        }

        private void newDeckTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (newDeckTextBox.Text == "")
            {
                newDeckTextBox.Text = "New Deck";
                newDeckTextBox.Foreground = Brushes.DarkGray;
            }
        }

        private void deckDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (deckDataGrid.SelectedItem != null)
            {
                selectedDeck = (Deck)deckDataGrid.SelectedItem;
                clearDeckButton.IsEnabled = true;
                if (twoPanelSelector.SelectedSymbolSet != null)
                    createDeckButton.IsEnabled = true;
            }
            else
            {
                selectedDeck = null;
                clearDeckButton.IsEnabled = false;
                createDeckButton.IsEnabled = false;
            }
        }

        private void addNewSymbolSetKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                if (newSymbolSetTextField.Text != null)
                    twoPanelSelector.addNewSymbolSet(newSymbolSetTextField.Text);
                newSymbolSetTextField.Text = "";
                newSymbolSetTextField.Foreground = Brushes.DarkGray;
                newSymbolSetTextField.Text = "New Symbol Set";
            }
        }

        private void addNewCategoryKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                if (newCategoryTextField.Text != null)
                    twoPanelSelector.addNewCategory(newCategoryTextField.Text);
                newCategoryTextField.Text = "";
                newCategoryTextField.Foreground = Brushes.DarkGray;
                newCategoryTextField.Text = "New Category";
            }

        }

        private void newDeckKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                if (newDeckTextBox.Foreground != Brushes.DarkGray)
                {
                    Deck newDeck = new Deck(newDeckTextBox.Text, false);
                    services.deckManager.Add(session, newDeck);

                    newDeckTextBox.Text = "New Deck";
                    newDeckTextBox.Foreground = Brushes.DarkGray;

                    updateDeckDataGrid();
                }
            }
        }

        private void deleteCategoryKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                twoPanelSelector.deleteSelectedCategory();
            }
        }
        private void mainMenuButton_Click_1(object sender, RoutedEventArgs e)
        {
            theAudioFiles.theFiles[2].Play();
            this.Close();
        }


        [TestFixture]
        public class DeckEditorTester
        {
            private SpotIt_Symbol_Set_Manager ssm;

            public DeckEditorTester() {
                Services s = new Services();
                s.WaitForAllDistributedServices();

                OperationResult result = s.accountManager.Login("admin", "admin");
                Session session = new Session();
                if (result.Success)
                    session = result.Data as Session;
                s.WaitForAllDistributedServices();

                ssm = new SpotIt_Symbol_Set_Manager(session, s);
            
            }

            [Test]
            public void TestDeckEditorInit()
            {
                Assert.NotNull(ssm);

            }

            [Test]
            private static void disableEnableTest(SpotIt_Symbol_Set_Manager ssm)
            {
                ssm.disableEditButton(null, null);

                Assert.IsFalse(ssm.editSetButton.IsEnabled);
                Assert.IsFalse(ssm.createDeckButton.IsEnabled);


                ssm.enableEditButton(null, null);

                Assert.IsTrue(ssm.editSetButton.IsEnabled);
                if (ssm.deckDataGrid.SelectedItem != null)
                {
                    Assert.IsTrue(ssm.createDeckButton.IsEnabled);
                }
                else
                {
                    Assert.IsFalse(ssm.createDeckButton.IsEnabled);
                }
            }
        }

    }
}
